//
//  HomeChoiceView.swift
//  GHSwiftProject
//
//  Created by Guanghui Liao on 4/11/18.
//  Copyright © 2018 liaoworking. All rights reserved.
//

import UIKit
import DropDown
import RxCocoa
import RxSwift

enum BtnPosition {
    case left
    case center
    case right
}

protocol HomeChoiceViewDelegate : class {
    func didSelectedItem(position:BtnPosition, index:Int)
}

class HomeChoiceView: UIView {

        private let leftDropDown = DropDown()
        private let centerDropDown = DropDown()
        private let rightDropDown = DropDown()
    
        private var leftBtn: UIButton!
        private var centerBtn: UIButton!
        private var rightBtn: UIButton!
    
        var leftData: [String]?
        var centerData: [String]?
        var rightData: [String]?
    
        weak var delegate : HomeChoiceViewDelegate?

        func configFakeData() {
            leftData = ["Beijing","Shanghai","Guangzhou","Shenzhen"]
            centerData = ["Beijing","Shanghai","Guangzhou","Shenzhen"]
            rightData = ["Beijing","Shanghai","Guangzhou","Shenzhen"]
        }
    
        let disposeBag = DisposeBag()
        override init(frame: CGRect) {
            super.init(frame: frame)
            configFakeData()
            configLeftBtn()
            configCenterBtn()
            configRightBtn()
        }
    
        func configLeftBtn(){
            leftBtn = UIButton(frame: CGRect(x: 0, y: 0, width: (self.frame.size.width * 0.333), height: self.frame.size.height))
            leftBtn.setTitle("左", for: .normal)
            leftBtn.backgroundColor = UIColor.red
            addSubview(leftBtn)
            leftBtn.rx.tap.subscribe(onNext:{
                self.leftDropDown.show()
            }).disposed(by: disposeBag)
            resturctDropDownView(dropdownView: leftDropDown, btn: leftBtn, data: leftData)
            // 点击事件
            leftDropDown.selectionAction = { [weak self] (index, item) in
                print("点击了\(index)")
                self?.delegate?.didSelectedItem(position: .left, index: index)
            }
        }

        func configCenterBtn() {
            centerBtn = UIButton(frame: CGRect(x: leftBtn.frame.maxX, y: 0, width: leftBtn.frame.size.width, height: leftBtn.frame.size.height))
            centerBtn.setTitle("中", for: .normal)
            centerBtn.backgroundColor = UIColor.yellow
            addSubview(centerBtn)
            centerBtn.rx.tap.subscribe(onNext:{
                
            }).disposed(by: disposeBag)
        }

        func configRightBtn() {
            rightBtn = UIButton(frame: CGRect(x: centerBtn.frame.maxX, y: 0, width: leftBtn.frame.size.width, height: leftBtn.frame.size.height))
            rightBtn.setTitle("右", for: .normal)
            rightBtn.backgroundColor = UIColor.green
            addSubview(rightBtn)
            rightBtn.rx.tap.subscribe(onNext:{
                
            }).disposed(by: disposeBag)

        }

        func resturctDropDownView(dropdownView:DropDown, btn:UIButton,data:[String]?){
            dropdownView.anchorView = btn
            dropdownView.dataSource = data ?? []
            dropdownView.backgroundColor = WHITE_COLOR
            dropdownView.bottomOffset = CGPoint(x: 0, y: leftBtn.bounds.height)

        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
}
